
*** Set Fixed Effects and Clustering
global fe "courtq"
global clusvar "partyr"

*** Set general controls
global exactcontrols "black hisp female blackfemale hispfemale  ageatfiling age2 age3 sh_hhpov med_grossrent rentowed multicase"


*******************************************
***** Table - Labor Market Outcomes *******   
*******************************************
replace samp=(sentDOL==1)

global earningsvars "lquartearn1 lquartearn2 lquartearn3 lquartearn4 lemployedquart1 lemployedquart2 lemployedquart3 lemployedquart4 lavg2yrearn lavg2yremply"
global lag2controls "$exactcontrols lquartearn9 lquartearn10 lquartearn11 lquartearn12 lemployedquart9 lemployedquart10 lemployedquart11 lemployedquart12 ly3t4earn ly3t4employ" 
global controls "$exactcontrols $earningsvars" 


capture frame drop labor
frame create labor str55 outcome str32 specification beta se N 

foreach spec in  ols rf iv meansd emean gmean {
	foreach out in q9t16earn q17t24earn q9t16emply q17t24emply y1earn y1employ y2earn y2employ ly1t2earn ly1t2employ {
	
	global controls "$exactcontrols $earningsvars" 

	if  "`out'"=="ly1t2earn" | "`out'"=="ly1t2employ" {
		global controls $lag2controls
	}

	if "`out'"!="ly1earn" & "`out'"!="ly2earn" & "`out'"!="ly1employ" & "`out'"!="ly1employ" & "`out'"=="ly1t2earn" &  "`out'"=="ly1t2employ" {
		global controls "$exactcontrols $earningsvars" 
	}
	
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post labor ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1, absorb($fe) absorb($clusvar)
		frame post labor ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="rf" {
		reghdfe `out' stringency $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post labor ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0
		frame post labor ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="gmean" {
		sum `out' if samp==1
		frame post labor ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="emean" {
		sum evictionorder if samp==1
		frame post labor ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
}
}

frame labor: saveold ${Inputs}/NY_labor_ivols, replace


frame change labor
replace specification=upper(specification)


replace outcome="EARNQ09Q16" if outcome=="q9t16earn"
replace outcome="EARNQ17Q24" if outcome=="q17t24earn"
replace outcome="EARNYEAR1" if outcome=="y1earn"
replace outcome="EARNYEAR2" if outcome=="y2earn"
replace outcome="EARNYEARL1_2" if outcome=="ly1t2earn"

replace outcome="EMPLQ09Q16" if outcome=="q9t16emply"
replace outcome="EMPLQ17Q24" if outcome=="q17t24emply"
replace outcome="EMPLYEAR1" if outcome=="y1employ"
replace outcome="EMPLYEAR2" if outcome=="y2employ"
replace outcome="EMPYEARL1_2" if outcome=="ly1t2employ"


replace specification=upper(specification)

saveold ${Inputs}/NY_labor_ivols, replace
frame change default


*****************************
**** Subgroup Effects:IV ****
*****************************

global controls "$exactcontrols $earningsvars" 

gen group=. 
gen g3=(female==1) 
gen g4=(female!=1) 
gen g5=(black==1) 
gen g6=(black!=1)
 
capture frame drop subgroup
frame create subgroup str55 outcome str32 group str32 specification  beta se N 

forvalues g=3/6 {
foreach spec in ols iv rf meansd {
	foreach out in  q9t16earn q17t24earn q9t16emply q17t24emply y1earn y1employ y2earn y2employ ly1t2earn ly1t2employ  {

	global controls "$exactcontrols $earningsvars" 
	
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 & g`g'==1 [pw=complier_weight], absorb($fe) absorb($clusvar)
		frame post subgroup ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1 & g`g'==1, absorb($fe) 		cluster(yrpartclass)
		frame post subgroup ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="rf" {
		reghdfe `out' stringency $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post subgroup ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}
	
	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0 & g`g'==1
		frame post subgroup ("`out'") ("`g'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}
}

frame subgroup: saveold "${Inputs}/NY_labor_ivols_subgroups.dta", replace
frame change subgroup

replace outcome="EARNQ09Q16" if outcome=="q9t16earn"
replace outcome="EARNQ17Q24" if outcome=="q17t24earn"
replace outcome="EARNYEAR1" if outcome=="y1earn"
replace outcome="EARNYEAR2" if outcome=="y2earn"

replace outcome="EMPLQ09Q16" if outcome=="q9t16emply"
replace outcome="EMPLQ17Q24" if outcome=="q17t24emply"
replace outcome="EMPLYEAR1" if outcome=="y1employ"
replace outcome="EMPLYEAR2" if outcome=="y2employ"

replace specification=upper(specification)

replace group="FEMALE" if group=="3"
replace group="MALE" if group=="4"
replace group="BLACK" if group=="5"
replace group="NOTBLACK" if group=="6"

saveold ${Inputs}/NY_labor_ivols_subgroups, replace
frame change default



*******************************************
***** Table - Homelessness Outcomes *******   
*******************************************

replace samp=all

global controls "$exactcontrols lhomelessapp1yr lhomelessapp2yr lshelt1yr lshelt2yr " 

global laghomelesscontrols "$exactcontrols lhomelessapp2yr lshelt2yr lhomelessapp3yr lshelt3yr lprehomelessapp3yr"
global laghomelesscontrols2 "$exactcontrols lhomelessapp3yr lshelt3yr lprehomelessapp3yr"

capture frame drop homeless
frame create homeless str55 outcome str32 specification beta se N 

foreach spec in ols rf iv meansd gmean emean {
	foreach out in q1t4shelter q5t8shelter q9t16shelter q17t24shelter q1t4homeless q5t8homeless q9t16homeless q17t24homeless lshelt1yr lhomelessapp1yr {

	if "`out'"=="lshelt1yr" | "`out'"=="lhomelessapp1yr" {
		global controls $laghomelesscontrols
	}
	
	if "`out'"!="lshelt1yr" & "`out'"!="lhomelessapp1yr" {
		global controls "$exactcontrols lhomelessapp1yr lshelt1yr " 
	}
	
	if  "`spec'"=="ols"  {
		reghdfe `out' evictionorder $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post homeless ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1, absorb($fe) absorb($clusvar)
		frame post homeless ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="rf" {
		reghdfe `out' stringency $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post homeless ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0
		frame post homeless ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="gmean" {
		sum `out' if samp==1
		frame post homeless ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="emean" {
		sum evictionorder if samp==1
		frame post homeless ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
}
}

frame homeless: saveold ${Inputs}/NY_homelessness_ivols, replace
frame change homeless

replace specification=upper(specification)

replace outcome="HOMEQ01Q04" if outcome=="q1t4homeless"
replace outcome="HOMEQ05Q08" if outcome=="q5t8homeless"
replace outcome="HOMEQ09Q16" if outcome=="q9t16homeless"
replace outcome="HOMEQ17Q24" if outcome=="q17t24homeless"
replace outcome="HOMEYEARL1" if outcome=="lhomelessapp1yr"
  
replace outcome="ESHUQ01Q04" if outcome=="q1t4shelter"
replace outcome="ESHUQ05Q08" if outcome=="q5t8shelter"
replace outcome="ESHUQ09Q16" if outcome=="q9t16shelter"
replace outcome="ESHUQ17Q24" if outcome=="q17t24shelter"
replace outcome="ESHUYEARL1" if outcome=="lshelt1yr"

frame homeless: saveold ${Inputs}/NY_homelessness_ivols, replace
frame change default

**** Subgroup: Homelessness

capture frame drop subgroupHomeless
frame create subgroupHomeless str55 outcome str32 group str32 specification  beta se N 

forvalues g=3/6 {
foreach spec in ols iv rf meansd {
	foreach out in  q1t4shelter q5t8shelter q9t16shelter q17t24shelt q1t4homeless q5t8homeless q9t16homeless q17t24homeless {

	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 & g`g'==1 [pw=complier_weight], absorb($fe) absorb($clusvar)
		frame post subgroupHomeless ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1 & g`g'==1, absorb($fe) 		cluster(yrpartclass)
		frame post subgroupHomeless ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="rf" {
		reghdfe `out' stringency $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post subgroupHomeless ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0 & g`g'==1
		frame post subgroupHomeless ("`out'") ("`g'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}
}

frame change subgroupHomeless

replace group="FEMALE" if group=="3"
replace group="MALE" if group=="4"
replace group="BLACK" if group=="5"
replace group="NOTBLACK" if group=="6"

replace outcome="HOMEQ01Q04" if outcome=="q1t4homeless"
replace outcome="HOMEQ05Q08" if outcome=="q5t8homeless"
replace outcome="HOMEQ09Q16" if outcome=="q9t16homeless"
replace outcome="HOMEQ17Q24" if outcome=="q17t24homeless"

replace outcome="ESHUQ01Q04" if outcome=="q1t4shelter"
replace outcome="ESHUQ05Q08" if outcome=="q5t8shelter"
replace outcome="ESHUQ09Q16" if outcome=="q9t16shelter"
replace outcome="ESHUQ17Q24" if outcome=="q17t24shelt"

replace specification=upper(specification)

saveold "${Inputs}/NY_homelessness_ivols_subgroups.dta", replace
frame change default



*******************************************
******* Table -  Moves Outcomes   *********   
*******************************************


global controls "$exactcontrols l1add l2add" 

capture frame drop moves
frame create moves str55 outcome str32 specification beta se N 

foreach spec in ols rf iv meansd emean gmean {
	foreach out in l1t2add amove0t1 amove2y amove3y amove1t2 amove2t3 amove3t4 amove5t6 {

	if "`out'"=="l1t2add" {
	global controls "$exactcontrols l3add"     
		
	}
	if "`out'"!="l1t2add" {
	global controls "$exactcontrols l1add l2add"    
		
	}
	
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 , absorb($fe) absorb($clusvar)
		frame post moves ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1, absorb($fe) absorb($clusvar)
		frame post moves ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="rf" {
		ivreghdfe `out' $controls stringency if samp==1, absorb($fe) absorb($clusvar)
		frame post moves ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		ivreghdfe `out' $controls (evictionorder =stringency)  if samp==1, absorb($fe) absorb($clusvar)
		sum `out' if e(sample)==1 & evictionorder==0
		frame post moves ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	
	if "`spec'"=="gmean" {
		sum `out' if samp==1
		frame post moves ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="emean" {
		sum evictionorder if samp==1
		frame post moves ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}

frame moves: saveold ${Inputs}/NY_moves_ivols.dta, replace

frame change moves
replace outcome="LMOVEY1Y2" if outcome=="l1t2add"
replace outcome="MOVEY0Y1" if outcome=="amove0t1"
replace outcome="MOVEY2" if outcome=="amove2y"
replace outcome="MOVEY3Y4" if outcome=="amove3t4"
replace outcome="MOVEY5Y6" if outcome=="amove5t6"

replace specification=upper(specification)

saveold ${Inputs}/NY_moves_ivols, replace
frame change default

*******************************************
******* Table - Subgroup Moves    *********   
*******************************************

capture frame drop subgroupMoves
frame create subgroupMoves str55 outcome str32 group str32 specification  beta se N 


forvalues g=1/6 {
foreach spec in ols iv rf meansd {
	foreach out in  amove0t1 amove2y amove3y amove1t2 amove2t3 amove3t4 amove5t6 {

	*Run the regression
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 & g`g'==1 , absorb($fe) absorb($clusvar)
		frame post subgroupMoves ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="rf" {
		ivreghdfe `out' $controls stringency if samp==1, absorb($fe) absorb($clusvar)
		frame post subgroupMoves ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1 & g`g'==1, absorb($fe) 		cluster(		yrpartclass)
		frame post subgroupMoves ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0 & g`g'==1
		frame post subgroupMoves ("`out'") ("`g'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}
}

frame change subgroupMoves

frame subgroupMoves: saveold "${Inputs}/NY_moves_ivols_subgroups.dta", replace
frame change subgroupMoves

replace group="FEMALE" if group=="3"
replace group="MALE" if group=="4"
replace group="BLACK" if group=="5"
replace group="NOTBLACK" if group=="6"

replace outcome="MOVEY0Y1" if outcome=="amove0t1"
replace outcome="MOVEY2" if outcome=="amove2y"
replace outcome="MOVEY3Y4" if outcome=="amove3t4"
replace outcome="MOVEY5Y6" if outcome=="amove5t6"
replace specification=upper(specification)

saveold "${Inputs}/NY_moves_ivols_subgroups.dta", replace


*******************************************
******* Table - NQUALITY Outcomes *********   
*******************************************
replace samp=all

global controls "$exactcontrols $courtvars l1add l2add" 

capture frame drop nqual
frame create nqual str55 outcome str32 specification beta se N 

foreach spec in ols rf iv meansd emean gmean  {
	foreach out in pov0t1 pov2y pov3t4 pov5t6  {

	
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1, absorb($fe) absorb($clusvar)
		frame post nqual ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1, absorb($fe) absorb($clusvar)
		frame post nqual ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="rf" {
		ivreghdfe `out' $controls stringency if samp==1, absorb($fe) absorb($clusvar)
		frame post nqual ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		ivreghdfe `out' $controls (evictionorder =stringency)  if samp==1, absorb($fe) absorb($clusvar)
		sum `out' if e(sample)==1 & evictionorder==0
		frame post nqual ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	
	if "`spec'"=="gmean" {
		sum `out' if samp==1
		frame post nqual ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	if "`spec'"=="emean" {
		sum evictionorder if samp==1
		frame post nqual ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}


frame nqual: saveold ${Inputs}/NY_nbhood_ivols.dta, replace
frame change nqual

replace outcome="NHOODPOVY0Y1" if outcome=="pov0t1"
replace outcome="NHOODPOVY3Y4" if outcome=="pov3t4"
replace outcome="NHOODPOVY5Y6" if outcome=="pov5t6"
replace outcome="NHOODPOVY2" if outcome=="pov2y"

replace specification=upper(specification)
saveold ${Inputs}/NY_nbhood_ivols.dta, replace
frame change default




capture frame drop subgroupnqual
frame create subgroupnqual str55 outcome str32 group str32 specification  beta se N 


forvalues g=1/6 {
foreach spec in ols iv rf meansd {
	foreach out in pov0t1 pov2y pov5t6 pov3t4   {

	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 & g`g'==1 [pw=complier_weight], absorb($fe) absorb($clusvar)
		frame post subgroupnqual ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="rf" {
		ivreghdfe `out' $controls stringency if samp==1, absorb($fe) absorb($clusvar)
		frame post subgroupnqual ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1 & g`g'==1, absorb($fe) 		cluster(		yrpartclass)
		frame post subgroupnqual ("`out'") ("`g'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if samp==1 & evictionorder==0 & g`g'==1
		frame post subgroupnqual ("`out'") ("`g'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}

	}
	
}
}

frame change subgroupnqual

frame subgroupnqual: saveold "${Inputs}/NY_nbhood_ivols_subgroups.dta", replace
frame change subgroupnqual

replace group="FEMALE" if group=="3"
replace group="MALE" if group=="4"
replace group="BLACK" if group=="5"
replace group="NOTBLACK" if group=="6"

replace outcome="NHOODPOVY0Y1" if outcome=="pov0t1"
replace outcome="NHOODPOVY3Y4" if outcome=="pov3t4"
replace outcome="NHOODPOVY5Y6" if outcome=="pov5t6"
replace specification=upper(specification)
replace outcome="NHOODPOVY2" if outcome=="pov2y"

saveold "${Inputs}/NY_nbhood_ivols_subgroups.dta", replace
frame change default




*****************************
***** Table - Lockout *******   
*****************************

capture frame drop lockout
frame create lockout str55 outcome str32 group str32 specification beta se N 

foreach out in lockout1yr lockout2yr {
foreach s in all g3 g4 g5 g6 {
foreach spec in ols rf iv  meansd {

	global controls "$exactcontrols" 
	
	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1 & `s'==1, absorb($fe) cluster($clusvar)
		frame post lockout ("`out'") ("`s'") ("`spec'") (_b[evictjudgment]) (_se[evictjudgment]) (e(N))
	}
	
	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1 & `s'==1, absorb($fe) cluster($clusvar)
		frame post lockout ("`out'") ("`s'") ("`spec'") (_b[evictjudgment]) (_se[evictjudgment]) (e(N))
	}

	if "`spec'"=="rf" {
		ivreghdfe `out' $controls stringency if samp==1 & `s'==1, absorb($fe) cluster($clusvar)
		frame post lockout ("`out'") ("`s'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="meansd" {
		sum `out' if e(sample)==1 & evictionorder==0 & `s'==1
		frame post lockout ("`out'") ("`s'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
	}
}
}
frame lockout: saveold ${Inputs}\NY_lockout_ivols.dta, replace
frame change lockout

replace outcome="Y1LOCKOUT" if outcome=="lockout1yr"
replace outcome="Y2LOCKOUT" if outcome=="lockout2yr"

replace group="FEMALE" if group=="g3"
replace group="MALE" if group=="g4"
replace group="BLACK" if group=="g5"
replace group="NOTBLACK" if group=="g6"
replace group="ALL" if group=="all"


replace specification=upper(specification)

frame lockout: saveold ${Inputs}\NY_lockout_ivols.dta, replace
frame change default





*******************************************
* Done